﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class LoginForm :  RelationsLMP.Control.Form.MetroDemoForms
    {
        public LoginForm()
        {
            InitializeComponent();
            txtUser.Text = "user1";
            txtPassdWord.Text = "123456";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string _UserName = txtUser.Text;
            string _UserPassWord = txtPassdWord.Text;
            Model.DB.tb_User _UserInfo;

            if (string.IsNullOrEmpty(_UserName) || string.IsNullOrEmpty(_UserPassWord))
            {
                MessageBox.Show(this, "用户名或密码不能为空，请重新输入！！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUser.Focus();
                return;
            }

            _UserInfo =  new DataAccess.tb_User().CheckUserInfo(_UserName,_UserPassWord);

            if (_UserInfo == null)
            {
                MessageBox.Show(this, "用户名或密码错误，请重新输入！！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUser.Focus(); 
                return;
            }
            else
            {
                //获取Token信息
                try
                {
                    //string[] paras = new string[1];
                    //paras[0] = $"\"type\":0,\"userName\":\"{_UserInfo.userName}\",\"passWord\":\"{_UserInfo.passWord}\",\"fingerPrint\":\"\",\"latitude\":0,\"longitude\":0,\"desc\":\"\"";
                    //paras[0] = "{" + paras[0] + "}";

                    //object result = Relations.Common.WebServiceHelper.InvokeWebService(Static.ParaInfo.webServiceURL, "userAuthentication", paras);
                    //string errorInfo;
                    //string _Result = string.Empty, _Token = string.Empty ;
                    //_Result = result.ToString();
                    //string _Key = "\"tonken\":\"";
                    //int _StartIndex = -1;

                    ////{"userInfo":{"userId":2,"roleId":66,"userName":"user1","tonken":"1c70bc4ddb4d47faa69adb4ab938d0c3"},"code":"00000","errorMsg":""}
                    //if (Static.Methods.webServiceCheckResultIsRight(result.ToString(), out errorInfo))
                    //{

                    //    _StartIndex = _Result.IndexOf(_Key);
                    //    _UserInfo.tonken = _Result.Substring(_StartIndex + _Key.Length, 32);

                    //    new DataAccess.tb_User().updateTonken(_UserInfo.userId, _UserInfo.tonken);

                    //    //MessageBox.Show(this, "获取Token成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //}
                    //else
                    //    MessageBox.Show(this, "获取Token失败，错误信息：" + errorInfo, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);


                    _UserInfo.tonken = Guid.NewGuid().ToString().Replace("-", "");

                    new DataAccess.tb_User().updateTonken(_UserInfo.userId, _UserInfo.tonken);

                    Static.ParaInfo.CurrentUser = _UserInfo;

                    MainForm mainForm = new MainForm();
                    mainForm.Event_FormClosing += MainForm_Event_FormClosing;
                    mainForm.Show();
                    mainForm.Focus();

                    this.Visible = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "获取Token失败，错误信息：" + ex.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void MainForm_Event_FormClosing(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
    }
}
